package com.ios.common.login;

import org.apache.log4j.Logger;

import com.ios.common.action.CommonAction;
import com.ios.common.login.action.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


public class CheckLoginInterceptor extends AbstractInterceptor {
	
	private static  Logger logger = Logger.getLogger(CheckLoginInterceptor.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		CommonAction action = (CommonAction) invocation.getAction();
		//如果是登录action不进行拦截
		if(action instanceof LoginAction ){//|| action instanceof RegisterAction
			logger.info("用户登录");
			return invocation.invoke();
		}
		Object vo = invocation.getInvocationContext().getSession().get(LoginAction.USER_INFO);
		//如果用户未登录
		if(vo == null){
			String htmlCode = "<script type=\"text/javascript\" language=\"javascript\">"
				+ "alert('用户未登录,请登录')</script>";
			invocation.getInvocationContext().getSession().put("alertMessage", htmlCode);
			return "user_login";//返回用户登录页面
		}
		//如果已经登录继续执行
		return invocation.invoke();
	}

}
